package com.myweb.demo.controller;

import com.myweb.demo.entity.Posts;
import com.myweb.demo.entity.User;
import com.myweb.demo.repository.PostsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PostsController {

    @Autowired
    private PostsRepository postsRepository;

   @GetMapping("/showposts/{posts_id}")
    public String show_posts(HttpServletRequest request, @PathVariable(name = "posts_id")Long post_id, Model model){
       HttpSession session = request.getSession();
       User user = (User) session.getAttribute("user");
       Integer userstatu = (Integer) session.getAttribute("userstatu");
       if (userstatu == null){
           userstatu = 0;
       }
       model.addAttribute("user",user);
       model.addAttribute("userstatu",userstatu);
       Posts posts = postsRepository.getPostsByPostsId(post_id);
       model.addAttribute("posts",posts);
       return "show_posts";
   }
}
